package com.sam.im.heiqun.uis.widgets;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.adapters.FragmentAdapter;

import java.util.ArrayList;
import java.util.List;

public class ChatBottomViews extends LinearLayout {
    private View baseView;
    private HeadIconSelectorView.OnHeadIconClickListener onHeadIconClickListener;
    public static final int FROM_CAMERA = 0;
    public static final int FROM_GALLERY = 2;
    public static final int FROM_PHRASE = 3;
    public static final int FROM_REDPACKET = 4;
    public static final int FROM_TRANSFER = 5;
    public static final int FROM_LOCATION = 6;
    public static final int FROM_FILE = 7;
    public static final int FROM_VIDEIO = 8;
    public static final int FROM_AA = 9;
    public static final int READ_DELETE = 10;
    public static final int READ_CALLVIDEO = 11;
    public static final int READ_CALLVOICE = 12;
    public static final int FROM_COLACTION = 13;
    public static final int FROM_FRIENDMP = 14;
    public static final int FROM_AUDIO_TXT = 15;
    public static final int FROM_VOICE_TXT = 16;
    public static final int FROM_VIDEO_TXT = 17;
    private LinearLayout imageGroup;
    private LinearLayout cameraGroup;
    private LinearLayout phraseGroup;
    private LinearLayout redpacketGroup;
    public LinearLayout transferGroup;
    private LinearLayout locationLayout;//定位
    private LinearLayout videoLayout;//视频
    private LinearLayout fileGrop; //文件
    private LinearLayout aacolaction;//AA收款
    private LinearLayout readDelete;//阅后即焚
    private LinearLayout collaction ; //收藏
    private LinearLayout bottomgrop;
    public LinearLayout ll_voice_call;//语音通话
    public LinearLayout callvideo,send_video;
    public LinearLayout callvoice;
    public LinearLayout audio_txt;//语音输入

    public LinearLayout friend_mp;

    public ViewPager viewPager;
    public FragmentAdapter adapter;
    public List<View> views = new ArrayList<>();
    private Mypageradapter mypageradapter;
    private List<View> guideViewList = new ArrayList<View>();

    public ChatBottomViews(Context context, AttributeSet attrs) {
        super(context, attrs);
        findView(context);
        init(context);
    }

    private void findView(Context context) {
        baseView = LayoutInflater.from(context).inflate(R.layout.layout_tongbaobottoms, this);
        viewPager = (ViewPager) baseView.findViewById(R.id.viewpager);
        bottomgrop = (LinearLayout) baseView.findViewById(R.id.linear_group);
        View view1 = LayoutInflater.from(context).inflate(R.layout.layout_tongbaobottom, this, false);
        View view2 = LayoutInflater.from(context).inflate(R.layout.layout_tongbaobottom2, this, false);
        views.add(view1);
        views.add(view2);
        imageGroup = (LinearLayout) view1.findViewById(R.id.image_bottom_group);
        cameraGroup = (LinearLayout) view1.findViewById(R.id.camera_group);
        phraseGroup = (LinearLayout) view1.findViewById(R.id.phrase_group);
        redpacketGroup = (LinearLayout) view1.findViewById(R.id.red_packet);
        transferGroup = (LinearLayout) view2.findViewById(R.id.transfer_accounts);
        locationLayout = (LinearLayout) view1.findViewById(R.id.location_layout);
        videoLayout = (LinearLayout) view1.findViewById(R.id.video_layout);
        fileGrop = (LinearLayout) view1.findViewById(R.id.send_file);
        aacolaction = (LinearLayout) view1.findViewById(R.id.aa_collection);
        readDelete = (LinearLayout) view1.findViewById(R.id.read_delete);
        callvideo = (LinearLayout)view2.findViewById(R.id.grou_callvideo);
        send_video = (LinearLayout)view2.findViewById(R.id.send_video);
        ll_voice_call = (LinearLayout)view2.findViewById(R.id.ll_voice_call);
        callvoice = (LinearLayout)view2.findViewById(R.id.call_voice);
        collaction = (LinearLayout) view2.findViewById(R.id.callaction_group);
        friend_mp = (LinearLayout) view2.findViewById(R.id.friend_mp);
        audio_txt = (LinearLayout) view2.findViewById(R.id.audio_txt);
        mypageradapter = new Mypageradapter();
        viewPager.setAdapter(mypageradapter);
        addGuideView(bottomgrop, context);
    }

    private void addGuideView(LinearLayout guideGroup, Context context) {
        guideViewList.clear();
        for (int i = 0; i < 2; i++) {
            Log.i("info","111=="+i);
            View view = new View(context);
            view.setBackgroundResource(R.drawable.selector_guide_bg);
            view.setSelected(i == 0 ? true : false);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.gudieview_width),
                    getResources().getDimensionPixelSize(R.dimen.gudieview_heigh));
            layoutParams.setMargins(10, 0, 10, 0);
            guideGroup.addView(view, layoutParams);
            guideViewList.add(view);
        }
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < guideViewList.size(); i++) {
                    guideViewList.get(i).setSelected(i == position ? true : false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void init(Context context) {
        aacolaction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_AA);
                }
            }
        });
        cameraGroup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_CAMERA);
                }
            }
        });
        imageGroup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_GALLERY);
                }
            }
        });
        phraseGroup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_PHRASE);
                }
            }
        });
        redpacketGroup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_REDPACKET);
                }
            }
        });
        transferGroup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_TRANSFER);
                }
            }
        });
        locationLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_LOCATION);
                }
            }
        });
        send_video.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_VIDEO_TXT);
                }
            }
        });
        ll_voice_call.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_VOICE_TXT);
                }
            }
        });
        fileGrop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_FILE);
                }
            }
        });
        videoLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_VIDEIO);
                }
            }
        });
        readDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(READ_DELETE);
                }
            }
        });
        callvideo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(READ_CALLVIDEO);
                }
            }
        });
        callvoice.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(READ_CALLVOICE);
                }
            }
        });
        collaction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_COLACTION);
                }
            }
        });
        friend_mp.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_FRIENDMP);
                }
            }
        });
        audio_txt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onHeadIconClickListener) {
                    onHeadIconClickListener.onClick(FROM_AUDIO_TXT);
                }
            }
        });

    }

    public void setOnHeadIconClickListener(
            HeadIconSelectorView.OnHeadIconClickListener onHeadIconClickListener) {
        this.onHeadIconClickListener = onHeadIconClickListener;
    }

    class Mypageradapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView(views.get(position));

        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(views.get(position));

            return views.get(position);
        }
    }
}
